<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
	<style type="text/css">
		body, html,#allmap {
			width: 100%;
			height: 100%;
			overflow: hidden;
			margin:0;
			padding:0;
			font-family:"微软雅黑";
		}
		.BMap_cpyCtrl.BMap_noprint.anchorBL{
			display: none;
		}
		div.anchorBL>a>img{
			display: none;
		}
	</style>
<title>地图加点工具</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
window.onload = function() {
	mapBox.loadJS();  //异步加载地图
}
// document.onkeydown = function(ev) {
// 	var ev = ev || event;
// 	if(ev.keyCode == 13){
// 		var position = mapBox.getPosition();
// 		alert('Lng:'+position.lng+","+'Lat:'+position.lat);
// 	}
// }
var mapBox = {
	map:'',
	menu:'',
	marker:'',
	address:"",
	isAdd:false,
	init:function(){
		this.map = new BMap.Map("allmap");// 实例化
		this.map.centerAndZoom("{$cfg.centerName}",12);// 地图中心
		this.map.enableScrollWheelZoom();//启用滚轮放大缩小
		// 比例尺
		// this.map.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT}));
		// 工具条
		this.map.addControl(new BMap.NavigationControl());
		// 城市列表
		var size = new BMap.Size(10, 20);
		this.map.addControl(new BMap.CityListControl({
		    anchor: BMAP_ANCHOR_TOP_RIGHT,
		    offset: size,
		}));
		// 右键菜单
		this.menu = new BMap.ContextMenu();
		var txtMenuItem = [
			{
				text:'添加',
				callback:function(e){
					mapBox.addPoint(e)
				}
			}
		];
		for(var i=0; i < txtMenuItem.length; i++){
			this.menu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
		}
		this.map.addContextMenu(this.menu);
	},
	loadJS:function(){
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "{$cfg.url}?v={$cfg.version}&ak={$cfg.ak}&callback=mapBox.init";
		document.body.appendChild(script);
	},
	addPoint:function(e){
		var curPoint = new BMap.Point(e.lng, e.lat); // 创建点坐标
		this.marker = new BMap.Marker(curPoint);	// 创建标注
		this.map.addOverlay(this.marker);           // 将标注添加到地图中
		this.marker.enableDragging();           	// 可拖拽
		this.map.removeContextMenu(this.menu);
		this.isAdd = true;
	},
	getPosition:function(){
		return this.marker.getPosition();
	},
	getLocation:function(lng,lat,fnSuccess) {
		var pt = new BMap.Point(lng,lat);
		var geoc = new BMap.Geocoder();
		geoc.getLocation(pt,function(res){
			fnSuccess(res);
		});
	}
}
</script>
